﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KinConquer.Packets.Handles
{
    public class Movement
    {
        public unsafe static void Handle(Byte* Data, KinSocket.Connection Client)
        {
            Structures.MovementPacket* Packet = (Structures.MovementPacket*)Data;
            Entities.Hero Hero = Program.GetHero(Client.Identifier);

            Byte direction = (Byte)(Packet->Direction % 8);

            UInt16 moveX = Hero.X;
            UInt16 moveY = Hero.Y;

            IncXY(direction, ref moveX, ref moveY);

            Hero.Angle = Entities.Calculations.Misc.Angle(Hero.X, Hero.Y, moveX, moveY);
            Hero.Action = 0;

            // Todo: finish
        }

        public static void IncXY(Byte Facing, ref ushort x, ref ushort y)
        {
            sbyte xi, yi;
            xi = yi = 0;
            switch (Facing)
            {
                case 3: xi = -1; yi = -1; break;
                case 7: xi = 1; yi = 1; break;
                case 5: xi = 1; yi = -1; break;
                case 1: xi = -1; yi = 1; break;
                case 2: xi = -1; break;
                case 0: yi = 1; break;
                case 4: yi = -1; break;
                case 6: xi = 1; break;
            }
            x = (ushort)(x + xi);
            y = (ushort)(y + yi);
        }
    }
}
